package com.kelvin.paopao.module.market.market.market;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.MarketAdapter;
import com.kelvin.paopao.injector.components.DaggerMarketComponent;
import com.kelvin.paopao.injector.modules.MarketModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.market.market.order.OrderActivity;
import com.kelvin.paopao.module.market.market.takeorder.TakeOrderActivity;
import com.kelvin.paopao.widgets.CustomToolbar;
import javax.inject.Inject;
import butterknife.BindView;


/**
 * Created by Administrator on 2017/5/10.
 */
public class MarketActivity extends BaseActivity<MarketPresenter> {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @Inject
    MarketAdapter adapter;
    @BindView(R.id.webView)
    WebView webView;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_market;
    }

    @Override
    protected void initInjector() {
        DaggerMarketComponent.builder().applicationComponent(
                getAppComponent()).marketModule(new MarketModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_order);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                OrderActivity.launch(MarketActivity.this);
                return true;
            }
        });

        WebSettings mSetting = webView.getSettings();
        mSetting.setJavaScriptEnabled(true);
        mSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);

        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.addJavascriptInterface(this, "jsinterface");
        webView.setWebViewClient(new WebViewClient() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                customToolbar.setTitle(title);
            }
        });

        webView.loadUrl("http://pao.lan361.com/shop/index.aspx");
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, MarketActivity.class);
        context.startActivity(intent);
    }

    /**
     * js调用此方法. 并且将参数传递过来
     *
     * @param
     */
    @android.webkit.JavascriptInterface
    public void gotoorder(final String id, final String number) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TakeOrderActivity.launch(MarketActivity.this, id, number.trim());
            }
        });
    }
}
